﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Interop;

namespace VisualTools.Controls
{
    public static class WindowManager
    {
        private static List<WeakReference> _windowCollection = new List<WeakReference>();

        public static WeakReference MainWindow { get; set; }

        public static void AddWindow(WpfWindow window)
        {
            if (_windowCollection.Where(p => p.Target as WpfWindow == window).Count() == 0)
            {
                _windowCollection.Add(new WeakReference(window));
                // Specify the main window
                if (MainWindow == null)
                {
                    if (System.Windows.Application.Current.MainWindow is WpfWindow)
                        MainWindow = new WeakReference((WpfWindow)System.Windows.Application.Current.MainWindow);
                    else
                        MainWindow = new WeakReference(window);
                }
            }
        }

        public static void RemoveWindow(WpfWindow window)
        {
            _windowCollection.RemoveAll(p => p.Target as WpfWindow == window);
        }

        public static void MinimizeAll()
        {
            foreach (var windowRef in _windowCollection)
            {
                (windowRef.Target as WpfWindow).WindowState = System.Windows.WindowState.Minimized;
            }
        }

        public static void MaximizeAll()
        {
            foreach (var windowRef in _windowCollection)
            {
                (windowRef.Target as WpfWindow).WindowState = System.Windows.WindowState.Maximized;
            }
        }

        public static void NormalizeAll()
        {
            foreach (var windowRef in _windowCollection)
            {
                (windowRef.Target as WpfWindow).WindowState = System.Windows.WindowState.Normal;
            }
        }

        public static void ShowAll()
        {
            // Show all windows except for the main window
            foreach (var windowRef in _windowCollection)
                if ((windowRef.Target as WpfWindow) != (MainWindow.Target as WpfWindow))
                {
                    (windowRef.Target as WpfWindow).Show();
                }
        }

        public static void HideAll()
        {
            // Hide all windows except for the main window
            foreach (var windowRef in _windowCollection)
                if ((windowRef.Target as WpfWindow) != (MainWindow.Target as WpfWindow))
                {
                    (windowRef.Target as WpfWindow).Hide();
                }
        }

        public static void ChangeTheme(Theme newTheme)
        {
            // Changes the theme of all windows
            foreach (var windowRef in _windowCollection)
                (windowRef.Target as WpfWindow).Theme = newTheme;
        }

        public static void CloseAll(bool includeMainWindow)
        {
            // Close all windows optionally including the MainWindow
            if (includeMainWindow && MainWindow != null)
                (MainWindow.Target as WpfWindow).Close();
            else
            {
                foreach (var windowRef in _windowCollection.ToList())
                    if ((windowRef.Target as WpfWindow) != (MainWindow.Target as WpfWindow))
                    {
                        RemoveWindow(windowRef.Target as WpfWindow);
                        (windowRef.Target as WpfWindow).Close();
                    }
            }
        }

        public static WpfWindow GetActiveWindow()
        {
            foreach (var windowRef in _windowCollection)
            {
                var window = windowRef.Target as WpfWindow;
                if (window.IsActive)
                    return window;
            }
            return null;
        }

        public static void DisableAll(bool includeMainWindow)
        {
            if (!includeMainWindow)
            {
                var subList = _windowCollection.Where(p => (p.Target as WpfWindow) != (MainWindow.Target as WpfWindow)).ToList();
                foreach (var windowRef in subList)
                {
                    (windowRef.Target as WpfWindow).IsEnabled = false;
                }
            }
            else
            {
                foreach (var windowRef in _windowCollection)
                {
                    (windowRef.Target as WpfWindow).IsEnabled = false;
                }
            }
        }

        public static void EnableAll()
        {
            foreach (var windowRef in _windowCollection)
            {
                (windowRef.Target as WpfWindow).IsEnabled = true;
            }
        }

        public static void ChangeResizeMode(System.Windows.ResizeMode mode)
        {
            foreach (var windowRef in _windowCollection)
            {
                (windowRef.Target as WpfWindow).ResizeMode = mode;
            }
        }
    }
}
